#!/bin/bash
set -e

Version=0.2.3

function rand(){
    min=$1
    max=$(($2-$min+1))
    num=$(($RANDOM+1000000000)) #增加一个10位的数再求余
    echo $(($num%$max+$min))
}

function build_deb(){
    echo "building deb"

    buildPath=$1

    mkdir -p deb/lark/usr/bin
    cp -f lark deb/lark/usr/bin/
    
    echo "Package: lark
Version: ${Version}
Section: utils
Priority: optional
Architecture: amd64
Depends: 
Installed-Size: 10M
Description: Jumei Service Mesh
Maintainer: [url=mailto:liangz1@jumei.com]liangz1#jumei.com" > deb/lark/DEBIAN/control
    
    ssh -l root 192.168.17.16 "rm -rf "${buildPath}"  && mkdir -p "${buildPath}
    rsync -rv  --exclude=.DS_Store --progress deb/lark root@192.168.17.16:${buildPath}
    ssh -l root 192.168.17.16 "cd "${buildPath}" && dpkg -b lark lark_"${Version}".deb"
    rsync -rv --progress root@192.168.17.16:${buildPath}/lark_${Version}.deb deb/
    ssh -l root 192.168.17.16 "rm -rf "${buildPath}
}

function build_rpm(){
    echo "building rpm"

    buildPath=$1

    echo "Name:	lark
Version: ${Version}
Release: 1%{?dist}
Summary: Jumei Service Mesh

Group:		Jumei/DevOps
License:	GPL
Packager:	liangz1
URL:		http://package.int.jumei.com/
BuildRoot:	%(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
Source1:   lark
Source2:   lark_start_shell

#BuildRequires:	
Requires:	start-stop-daemon

%description
jumei-lark

%prep


%build


%install
rm -rf %{buildroot}
mkdir -p \$RPM_BUILD_ROOT/usr/bin/
mkdir -p \$RPM_BUILD_ROOT/etc/init.d/
cp %{SOURCE1}  \$RPM_BUILD_ROOT/usr/bin/
cp %{SOURCE2}  \$RPM_BUILD_ROOT/etc/init.d/lark
 



%clean
rm -rf %{buildroot}



%post
mkdir -p home/logs/lark/
chmod +x /usr/bin/lark
chmod +x /etc/init.d/lark
/sbin/chkconfig --add lark
/sbin/chkconfig lark on
/etc/init.d/lark start


%preun
/etc/init.d/lark stop
/sbin/chkconfig lark off
/sbin/chkconfig --del lark

%postun

exit 0

%files
%defattr(755,root,root,755)
/etc/init.d/lark
/usr/bin/lark

# config
%defattr(0644,root,root,0755)
%config(noreplace)

%doc



%changelog" > rpm/package.spec

    ssh 192.168.17.153  -p8822 -lliangz1 "rm -rf "${buildPath}"  && mkdir -p "${buildPath}"/{SOURCES,BUILD,RPMS,SPECS,SRPMS}"
    
    rsync -rv -e 'ssh -p 8822 ' --exclude=.DS_Store --progress lark rpm/package.spec rpm/lark_start_shell liangz1@192.168.17.153:${buildPath}"/SOURCES"
    
    echo "cd "${buildPath}" && rpmbuild -bb --define '%_topdir " ${buildPath}"' "${buildPath}"/SOURCES/package.spec"
    ssh 192.168.17.153  -p8822 -lliangz1 "cd "${buildPath}" && sudo rpmbuild -bb --define '%_topdir " ${buildPath}"' "${buildPath}"/SOURCES/package.spec"
    
    
    
    rsync -rv -e 'ssh -p 8822 ' --exclude=.DS_Store --progress liangz1@192.168.17.153:${buildPath}"/RPMS/x86_64/lark-"${Version}"-1.el6.x86_64.rpm" rpm/
    
    ssh 192.168.17.153 -p8822 -lliangz1 "sudo rm -rf "${buildPath}
}

#check if git clean
STATUS=$(command git status --porcelain 2> /dev/null | tail -n1)
if [[ -n $STATUS ]]; then
    echo "submit all first"
    #exit
fi

BuildTime=`date -u '+%Y-%m-%d_%I:%M:%S%p'`
GitHash=`git rev-parse --short HEAD`
GoVersion=`go version`
LDFlag="-w -s -X lark/conf.GitHash=${GitHash} -X lark/conf.BuildTime=${BuildTime} -X lark/conf.Version=${Version}"

echo "Version: ${Version}"
echo "GitHash: ${GitHash}"
echo "BuildTime: ${BuildTime}"
echo "`go version`"

#pack a release version.
echo "building program"
env GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="${LDFlag}"

buildPath="/tmp/lark_building_"$(rand 400000 500000)
build_deb $buildPath
build_rpm $buildPath
